package com.czx.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.sun
 * @date 2023/6/24 21:31
 */

@Configuration
@Slf4j
public class DeadConfig {

    // 声明业务Exchange
    @Bean
    public FanoutExchange businessExchange() {
        return new FanoutExchange("businessExchange");
    }

    // 声明业务队列A
    @Bean
    public Queue businessQueue() {
        Map<String, Object> args = new HashMap<>();
        //当发生异常，或者是消费者NACK时，才会执行这个交换机
//       x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadLetterExchange");
//       x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", "dle.err");

        return new Queue("businessQueue", true, false, false, args);
    }

    // 声明业务队列A绑定关系
    @Bean
    public Binding businessBinding() {
        return BindingBuilder.bind(businessQueue()).to(businessExchange());
    }

    //声明死信Exchange
    @Bean
    public TopicExchange deadLetterExchange(){
        return new TopicExchange("deadLetterExchange");
    }

    // 声明死信队列A
    @Bean
    public Queue deadLetterQueue(){
        return new Queue("dle-queue");
    }

    @Bean
    public Binding deadLetterQueueBinding(Queue deadLetterQueue, TopicExchange deadLetterExchange){
        return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange).with("dle.*");
    }
}
